<template>
    <!-- 公共头部组件 -->
    <div class="header">
        <!-- 折叠按钮 -->
        <div class="collapse-btn" @click="collapseChage">
            <!-- 通过v-if和v-else来判定侧边栏打开状态 -->
            <i v-if="!collapse" class="el-icon-s-fold"></i>
            <i v-else class="el-icon-s-unfold"></i>
        </div>


        <!-- 系统logo -->
        <div class="logo">考勤管理系统</div>


        <!-- 头部组件右边功能区 -->
        <div class="header-right">
            <div class="header-user-con">


                <!-- 全屏显示 -->
                <div class="btn-fullscreen" @click="handleFullScreen">
                 <!--tooltip提供了两个主题：dark和light，通过 effect 设置主题 -->
                    <!-- 通过三元表达式来设置不同的文字提示，placement属性控制文字提示出现的位置 -->
                    <el-tooltip effect="dark" :content="fullscreen?`取消全屏`:`全屏`" placement="bottom">
                        <i class="el-icon-rank"></i>
                    </el-tooltip>
                </div>

                <!-- 用户头像 -->
                <div class="user-avator">
                    <img src="../../assets/img/img.jpg" />
                </div>


                <!-- 用户名下拉菜单：trigger属性配置 click 激活
                command：点击菜单项触发的事件回调 -->
                <el-dropdown class="user-name" trigger="click" @command="handleCommand">
                    <span class="el-dropdown-link">
                        {{emp_name}}
                        <i class="el-icon-caret-bottom"></i>
                    </span>
                    <!-- slot设置下拉列表 -->
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item divided command="loginout">退出登录</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>


            </div>
        </div>
    </div>
</template>
<script>


// 加载bus实现组件通信
    import bus from '../../utils/bus';


    export default {
        data() {
            return {
                collapse: false,
                fullscreen: false,
                name: 'TestUser',
                message: 2
            };
        },


        // 监控自定义属性
        computed: {
            username() {
                return this.$store.state.username;
            },
            emp_name() {
                return this.$store.state.emp_name;
            },
            password() {
                return this.$store.state.password;
            },
            token() {
                return this.$store.state.token;
            }
        },


        methods: {


            // 用户名下拉菜单选择事件
            handleCommand(command) {
                
                var that = this;
                //退出登录事件
                if (command == 'loginout') {
                    // 跳转到登录页面
                    fetch('http://localhost:9095/logout', {
                        method: 'POST',
                        body: JSON.stringify({username: that.username, 
                                            password: that.password,
                                            Token: that.token}),
                        headers: new Headers({
                            'Content-Type': 'application/json'
                        })
                    }).then(res => {return res.json(); })
                    .then(json => {console.log('logoutStatus', json.logoutStatus);
                                    if (json.logoutStatus == 'success') {
                                        that.$store.commit("username", null);
                                        that.$store.commit("password", null);
                                        that.$store.commit("emp_name", null);
                                        that.$store.commit("department", null);
                                        that.$store.commit("department_id", null);
                                        that.$store.commit("auth", null);
                                        that.$router.push('/login');
                                    }})
                    .catch(err => {console.log('请求错误', err)});
                }
            },


            // 侧边栏折叠
            collapseChage() {
                this.collapse = !this.collapse;
                // 通过bus发送信息
                bus.$emit('collapse', this.collapse);
            },


            // 全屏事件
            handleFullScreen() {
                // 获取文档对象的根元素
                let element = document.documentElement;
                if (this.fullscreen) {
                    // 设置不同浏览器下的退出全屏模式 
                    // -webkit- 兼容chrome 和 safari
                    // -moz- 兼容firefox
                    // -ms- 兼容IE浏览器
                    // -o- 兼容opera
                    if (document.exitFullscreen) {
                        document.exitFullscreen();
                    } else if (document.webkitCancelFullScreen) {
                        document.webkitCancelFullScreen();
                    } else if (document.mozCancelFullScreen) {
                        document.mozCancelFullScreen();
                    } else if (document.msExitFullscreen) {
                        document.msExitFullscreen();
                    }
                } else {
                    // 设置不同浏览器下的进入全屏模式
                    if (element.requestFullscreen) {
                        element.requestFullscreen();
                    } else if (element.webkitRequestFullScreen) {
                        element.webkitRequestFullScreen();
                    } else if (element.mozRequestFullScreen) {
                        element.mozRequestFullScreen();
                    } else if (element.msRequestFullscreen) {
                        // IE11
                        element.msRequestFullscreen();
                    }
                }
                this.fullscreen = !this.fullscreen;
            }
        },


        // 初始化页面完成后，对页面可见区域宽度进行判定，如果页面宽度小于1500，则触发 collapseChage 方法。
        mounted() {
            if (document.body.clientWidth < 1500) {
                this.collapseChage();
            }
        }
    };
</script>


<!-- CSS样式 -->
<style scoped>
    .header {
        position: relative;
        box-sizing: border-box;
        width: 100%;
        height: 70px;
        font-size: 22px;
        color: #fff;
        width: 100%;
    }
    .collapse-btn {
        float: left;
        padding: 0 21px;
        cursor: pointer;
        line-height: 70px;
    }
    .header .logo {
        float: left;
        width: 250px;
        line-height: 70px;
    }
    .header-right {
        float: right;
        padding-right: 50px;
    }
    .header-user-con {
        display: flex;
        height: 70px;
        align-items: center;
    }
    .btn-fullscreen {
        transform: rotate(45deg);
        margin-right: 5px;
        font-size: 24px;
    }
    .btn-fullscreen {
        position: relative;
        width: 30px;
        height: 30px;
        text-align: center;
        border-radius: 15px;
        cursor: pointer;
    }
    .btn-bell-badge {
        position: absolute;
        right: 0;
        top: -2px;
        width: 8px;
        height: 8px;
        border-radius: 4px;
        background: #f56c6c;
        color: #fff;
    }
    .btn-bell .el-icon-bell {
        color: #fff;
    }
    .user-name {
        margin-left: 10px;
    }
    .user-avator {
        margin-left: 20px;
    }
    .user-avator img {
        display: block;
        width: 40px;
        height: 40px;
        border-radius: 50%;
    }
    .el-dropdown-link {
        color: #fff;
        cursor: pointer;
    }
    .el-dropdown-menu__item {
        text-align: center;
    }
</style>